package leetcode_201_300;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class LeeCode_290 {
    public static void main(String[] args) {
        System.out.println(wordPattern("abba", "dog cat cat dog"));
    }
    private static boolean wordPattern(String pattern, String s) {
        String[] ss = s.split(" ");
        if (pattern.length() != ss.length)
            return false;
        Map<Character, String> map1 = new HashMap<>();
        Map<String, Character> map2 = new HashMap<>();
        for (int i = 0; i < ss.length; i++) {
            char key = pattern.charAt(i);
            String val = ss[i];
            if (!map1.containsKey(key) && !map2.containsKey(val)){
                map1.put(key, val);
                map2.put(val, key);
                continue;
            }
            if (map1.containsKey(key) && map2.containsKey(val) && map1.get(key).equals(val)
                    && map2.get(val).equals(key)){
                continue;
            }
            return false;
        }
        return true;
    }
}
